﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Litografia.BusinessEntities;
using Nortec.Security;

namespace Litografia.DataObjects
{
    public sealed class DOOT : DOBase
    {
        #region Declarations

        private const string GetParametricDataSP = "OTParametrosGet";
        private const string GetOTSP = "OTGet";
        private const string GetOTCotizacionSP = "OTCotizacionGet";
        private const string GetEstadoSP = "OTEstadoGet";
        private const string GetOTListSP = "OTGetList";
        private const string SaveOTEntregaSP = "OTEntregaSave";
        private const string SaveOTDetalleSP = "OTDetalleSave";
        private const string SaveOTMaterialSP = "OTMaterialSave";
        private const string SaveOTGastoSP = "OTGastoSave";
        private const string CloseEntregaSP = "OTEntregaClose";
        private const string HorasLaboradasSP = "HorasLaboradasGet";
        private const string InsertOTCotizacion = "OTCotizacionInsert";
        private const string UpdateOTCotizacion = "OTCotizacionUpdate";
        private const string InsertOTEntregaCotizacion = "OTEntregaCotizacionInsert";
        private const string UpdateOTEntregaCotizacion = "OTEntregaCotizacionUpdate";
        private const string DeleteOTEntregaCotizacion = "OTEntregaCotizacionDelete";
        private const string InsertOTMaterialCotizacion = "OTMaterialCotizacionInsert";
        private const string UpdateOTMaterialCotizacion = "OTMaterialCotizacionUpdate";
        private const string DeleteOTMaterialCotizacion = "OTMaterialCotizacionDelete";
        #endregion
        
        #region Methods: Public

        public static BEOT GetParametricData()
        {
            var command = DB.GetStoredProcCommand(GetParametricDataSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@SucursalId", DbType.Int32, NortecMembership.BranchId);

            var dataset = DB.ExecuteDataSet(command);

            return ConvertRawToEntity(dataset);
        }
        
        public static BEOT Get(string ordenTrabajoNumber)
        {
            var command = DB.GetStoredProcCommand(GetOTSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@NumeroOT", DbType.String, ordenTrabajoNumber);

            var dataset = DB.ExecuteDataSet(command);

            return ConvertRawToEntity(dataset);
        }

        public static BEOT GetOTCotizacion(string ordenTrabajoNumber)
        {
            var command = DB.GetStoredProcCommand(GetOTCotizacionSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@NumeroOT", DbType.String, ordenTrabajoNumber);

            var dataset = DB.ExecuteDataSet(command);

            return ConvertRawToEntity(dataset);
        }

        public static IList<BEEntity> GetEstados()
        {
            var result = new List<BEEntity>();
            var command = DB.GetStoredProcCommand(GetEstadoSP);
            var dataset = DB.ExecuteDataSet(command);

            if (dataset != null && dataset.Tables.Count == 1)
                foreach (DataRow row in dataset.Tables[0].Rows)
                    result.Add(CreateEntityFromDataRow(row));

            return result;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public static void SaveOTCotizacion(BEOT cotizacionOT)
        {
            using (var conn = DB.CreateConnection())
            {
                conn.Open();
                var t = conn.BeginTransaction();

                try
                {
                    // cotizacionOT.EnforceConstraints = false;
                    var insertOTCommand = DB.GetStoredProcCommand(InsertOTCotizacion);
                    DB.AddInParameter(insertOTCommand, "EmpresaId", DbType.Int32, "EmpresaId", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "NumeroOT", DbType.String, "NumeroOT", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "ArticuloId", DbType.String, "ProductoId", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "ClienteId", DbType.String, "ClienteId", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "FechaEntrega", DbType.DateTime, "FechaEntrega", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Referencia", DbType.String, "Referencia", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "VendedorId", DbType.Int32, "VendedorId", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "PruebaColor", DbType.Boolean, "PruebaColor", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Laser", DbType.Boolean, "Laser", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "TrabajoNuevo", DbType.Boolean, "TrabajoNuevo", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "ReimpresionCambios", DbType.Boolean, "ReimpresionCambios", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "ReimpresionExacta", DbType.Boolean, "ReimpresionExacta", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Cantidad", DbType.Decimal, "Cantidad", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "TamanoFinal", DbType.String, "TamanoFinal", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Impresion2Lados", DbType.Boolean, "Impresion2Lados", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "FullColor", DbType.Boolean, "FullColor", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "DosColores", DbType.Boolean, "DosColores", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "UnColor", DbType.Boolean, "UnColor", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Pantone", DbType.Boolean, "Pantone", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Otros", DbType.Boolean, "Otros", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "AcabadosFinales", DbType.String, "AcabadosFinales", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "HorasAcabados", DbType.Decimal, "HorasAcabados", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "HorasPrensas", DbType.Decimal, "HorasPrensas", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "GastosIndirectos", DbType.Decimal, "GastosIndirectos", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Factura", DbType.String, "Factura", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Comentarios", DbType.String, "Comentarios", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "CantidadDommies", DbType.Decimal, "CantidadDommies", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Observaciones", DbType.String, "Observaciones", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "Boleta", DbType.String, "Boleta", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "VistoBuenoMaquina", DbType.Boolean, "VistoBuenoMaquina", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "DibujoMontaje", DbType.String, "DibujoMontaje", DataRowVersion.Current);
                    DB.AddInParameter(insertOTCommand, "EstadoId", DbType.Int32, "EstadoId", DataRowVersion.Current);

                    var updateOTCommand = DB.GetStoredProcCommand(UpdateOTCotizacion);
                    DB.AddInParameter(updateOTCommand, "Id", DbType.Int32, "Id", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "FechaEntrega", DbType.DateTime, "FechaEntrega", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "ClienteId", DbType.String, "ClienteId", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "ArticuloId", DbType.String, "ProductoId", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "VendedorId", DbType.Int32, "VendedorId", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Referencia", DbType.String, "Referencia", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "PruebaColor", DbType.Boolean, "PruebaColor", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Laser", DbType.Boolean, "Laser", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "TrabajoNuevo", DbType.Boolean, "TrabajoNuevo", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "ReimpresionCambios", DbType.Boolean, "ReimpresionCambios", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "ReimpresionExacta", DbType.Boolean, "ReimpresionExacta", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Cantidad", DbType.Decimal, "Cantidad", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "TamanoFinal", DbType.String, "TamanoFinal", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Impresion2Lados", DbType.Boolean, "Impresion2Lados", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "FullColor", DbType.Boolean, "FullColor", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "DosColores", DbType.Boolean, "DosColores", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "UnColor", DbType.Boolean, "UnColor", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Pantone", DbType.Boolean, "Pantone", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Otros", DbType.Boolean, "Otros", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "AcabadosFinales", DbType.String, "AcabadosFinales", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "HorasAcabados", DbType.Decimal, "HorasAcabados", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "HorasPrensas", DbType.Decimal, "HorasPrensas", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "GastosIndirectos", DbType.Decimal, "GastosIndirectos", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Factura", DbType.String, "Factura", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Comentarios", DbType.String, "Comentarios", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "CantidadDommies", DbType.Decimal, "CantidadDommies", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Observaciones", DbType.String, "Observaciones", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "Boleta", DbType.String, "Boleta", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "VistoBuenoMaquina", DbType.Boolean, "VistoBuenoMaquina", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "DibujoMontaje", DbType.String, "DibujoMontaje", DataRowVersion.Current);
                    DB.AddInParameter(updateOTCommand, "EstadoId", DbType.Int32, "EstadoId", DataRowVersion.Current);

                    DB.UpdateDataSet(cotizacionOT, "OrdenTrabajo", insertOTCommand, updateOTCommand, null, t);

                    var insertOTEntregaCommand = DB.GetStoredProcCommand(InsertOTEntregaCotizacion);
                    DB.AddInParameter(insertOTEntregaCommand, "OTId", DbType.Int32, "OTId", DataRowVersion.Current);
                    DB.AddInParameter(insertOTEntregaCommand, "Orden", DbType.Int32, "Orden", DataRowVersion.Current);
                    DB.AddInParameter(insertOTEntregaCommand, "UnidadesSolicitadas", DbType.Decimal, "UnidadesSolicitadas", DataRowVersion.Current);
                    DB.AddInParameter(insertOTEntregaCommand, "FechaEntrega", DbType.DateTime, "FechaEntrega", DataRowVersion.Current);
                    DB.AddInParameter(insertOTEntregaCommand, "EstadoId", DbType.Int32, "EstadoId", DataRowVersion.Current);

                    var updateOTEntregaCommand = DB.GetStoredProcCommand(UpdateOTEntregaCotizacion);
                    DB.AddInParameter(updateOTEntregaCommand, "Id", DbType.Int32, "Id", DataRowVersion.Current);
                    DB.AddInParameter(updateOTEntregaCommand, "Orden", DbType.Int32, "Orden", DataRowVersion.Current);
                    DB.AddInParameter(updateOTEntregaCommand, "UnidadesSolicitadas", DbType.Decimal, "UnidadesSolicitadas", DataRowVersion.Current);
                    DB.AddInParameter(updateOTEntregaCommand, "FechaEntrega", DbType.DateTime, "FechaEntrega", DataRowVersion.Current);
                    DB.AddInParameter(updateOTEntregaCommand, "EstadoId", DbType.Int32, "EstadoId", DataRowVersion.Current);


                    DbCommand deleteOTEntregaCotizadoCommand = DB.GetStoredProcCommand(DeleteOTEntregaCotizacion);
                    DB.AddInParameter(deleteOTEntregaCotizadoCommand, "Id", DbType.Int32, "Id", DataRowVersion.Current);

                    DB.UpdateDataSet(cotizacionOT, "OTEntrega", insertOTEntregaCommand, updateOTEntregaCommand, deleteOTEntregaCotizadoCommand, t);
                    
                    DbCommand insertOTMaterialCotizadoCommand = DB.GetStoredProcCommand(InsertOTMaterialCotizacion);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "OTId", DbType.Int32, "OTId", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "Material", DbType.String, "Material", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "Papel", DbType.String, "Papel", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "TamanoPliegoMaterial", DbType.String, "TamanoPliegoMaterial", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "TamanoPliegoPrensa", DbType.String, "TamanoPliegoPrensa", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "Cantidad", DbType.Decimal, "Cantidad", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "Aumento", DbType.Decimal, "Aumento", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "PliegosTotal", DbType.Decimal, "PliegosTotal", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "Montaje", DbType.Decimal, "Montaje", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "PliegosPrensaTotal", DbType.Decimal, "PliegosPrensaTotal", DataRowVersion.Current);
                    DB.AddInParameter(insertOTMaterialCotizadoCommand, "NoCotizado", DbType.Decimal, "NoCotizado", DataRowVersion.Current);
                    

                    var updateOTMaterialCotizadoCommand = DB.GetStoredProcCommand(UpdateOTMaterialCotizacion);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "Id", DbType.Int32, "Id", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "Material", DbType.String, "Material", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "Papel", DbType.String, "Papel", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "TamanoPliegoMaterial", DbType.String, "TamanoPliegoMaterial", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "TamanoPliegoPrensa", DbType.String, "TamanoPliegoPrensa", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "Cantidad", DbType.Decimal, "Cantidad", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "Aumento", DbType.Decimal, "Aumento", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "PliegosTotal", DbType.Decimal, "PliegosTotal", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "Montaje", DbType.Decimal, "Montaje", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "PliegosPrensaTotal", DbType.Decimal, "PliegosPrensaTotal", DataRowVersion.Current);
                    DB.AddInParameter(updateOTMaterialCotizadoCommand, "NoCotizado", DbType.Decimal, "NoCotizado", DataRowVersion.Current);


                    var deleteOTMaterialCotizadoCommand = DB.GetStoredProcCommand(DeleteOTMaterialCotizacion);
                    DB.AddInParameter(deleteOTMaterialCotizadoCommand, "Id", DbType.Int32, "Id", DataRowVersion.Current);

                    DB.UpdateDataSet(cotizacionOT, "OTMaterialCotizado", insertOTMaterialCotizadoCommand, updateOTMaterialCotizadoCommand, deleteOTMaterialCotizadoCommand, t);

                    t.Commit();
                }
                catch
                {
                    t.Rollback();
                    throw;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public static void Save(BEOT orden)
        {
            using (var conn = DB.CreateConnection())
            {
                conn.Open();
                var t = conn.BeginTransaction();

                try
                {
                    foreach (var entrega in orden.OTEntrega)
                    {
                        var entregaId = SaveOTEntrega(entrega, t);
                        foreach (var detalle in entrega.GetOTDetalleRows())
                            SaveOTDetalle(detalle, entregaId, t);

                        foreach (var material in entrega.GetOTMaterialRows())
                            SaveOTMaterial(material, entregaId, t);

                        foreach (var gasto in orden.OTGasto)
                            SaveOTGasto(gasto, entregaId, t);
                    }
                    t.Commit();
                }
                catch
                {
                    t.Rollback();
                    throw;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public static void SaveRequisicion(BERequisicion requisicion)
        {
            using (var conn = DB.CreateConnection())
            {
                conn.Open();
                var t = conn.BeginTransaction();

                try
                {
                    var reqId = SaveReqEncabezado(requisicion.Requisicion[0], t);
                    requisicion.Requisicion[0].Id = reqId;
                    foreach (var detalle in requisicion.RequisicionDetalle)
                        SaveReqDetalle(detalle, reqId, t);
                    t.Commit();
                }
                catch
                {
                    t.Rollback();
                    throw;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public static void CloseEntrega(int entregaId)
        {
            var command = DB.GetStoredProcCommand(CloseEntregaSP);
            DB.AddInParameter(command, "@empresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@entregaId", DbType.Int32, entregaId);
            DB.AddInParameter(command, "@usuarioId", DbType.String, NortecMembership.Identity.Name);
            DB.ExecuteNonQuery(command);
        }

        public static BEHorasLaboradasReporte GetHorasLaboradas(string operarios, DateTime? beginDate, DateTime? endDate)
        {
            var command = DB.GetStoredProcCommand(HorasLaboradasSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@OperarioLista", DbType.String, operarios);
            DB.AddInParameter(command, "@fechaInicial", DbType.DateTime, beginDate ?? Convert.DBNull);
            DB.AddInParameter(command, "@fechaFinal", DbType.DateTime, endDate ?? Convert.DBNull);

            var dataset = DB.ExecuteDataSet(command);

            return ConvertRawToHorasLaboradas(dataset);
        }

        public static IList<BEEntity> GetOTList()
        {
            var result = new List<BEEntity>();
            var command = DB.GetStoredProcCommand(GetOTListSP);
            var dataset = DB.ExecuteDataSet(command);

            if (dataset != null && dataset.Tables.Count == 1)
                foreach (DataRow row in dataset.Tables[0].Rows)
                    result.Add(CreateEntityFromDataRow(row));
                
            return result;
        }
        
        #endregion       

        #region Methods Non-Public

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "t"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "row")]
        private static int SaveReqEncabezado(BERequisicion.RequisicionRow row, DbTransaction t)
        {
            //var command = DB.GetStoredProcCommand(SaveReqEncabezadoSP);

            //DB.AddInParameter(command, "@id", DbType.Int32, row.Id <= 0 ? Convert.DBNull : row.Id);
            //DB.AddInParameter(command, "@empresaId", DbType.Int32, NortecMembership.CompanyId);
            //DB.AddInParameter(command, "@fechaInicial", DbType.DateTime, row.IsFechaInicialNull() ? Convert.DBNull : row.FechaInicial);
            //DB.AddInParameter(command, "@fechaFinal", DbType.DateTime, row.IsFechaFinalNull() ? Convert.DBNull : row.FechaFinal);
            //DB.AddInParameter(command, "@FechaEntrega", DbType.DateTime, row.IsFechaEntregaNull() ? Convert.DBNull : row.FechaEntrega);
            //DB.AddInParameter(command, "@numeroOT", DbType.String, row.NumeroOT);
            //DB.AddInParameter(command, "@clienteId", DbType.String, row.IsClienteIdNull() ? Convert.DBNull : row.ClienteId);
            //DB.AddInParameter(command, "@articuloId", DbType.String, row.IsProductoIdNull() ? Convert.DBNull : row.ProductoId);
            //DB.AddInParameter(command, "@vendedorId", DbType.Int32, row.IsVendedorIdNull() ? Convert.DBNull : row.VendedorId);
            //DB.AddInParameter(command, "@PruebaColor", DbType.Boolean, row.IsPruebaColorNull() ? Convert.DBNull : row.PruebaColor);
            //DB.AddInParameter(command, "@Laser", DbType.Boolean, row.IsLaserNull() ? Convert.DBNull : row.Laser);
            //DB.AddInParameter(command, "@TrabajoNuevo", DbType.Boolean, row.IsTrabajoNuevoNull() ? Convert.DBNull : row.TrabajoNuevo);
            //DB.AddInParameter(command, "@ReimpresionCambios", DbType.Boolean, row.IsReimpresionCambiosNull() ? Convert.DBNull : row.ReimpresionCambios);
            //DB.AddInParameter(command, "@ReimpresionExacta", DbType.Boolean, row.IsReimpresionExactaNull() ? Convert.DBNull : row.ReimpresionExacta);
            //DB.AddInParameter(command, "@Cantidad", DbType.Decimal, row.IsCantidadNull() ? Convert.DBNull : row.Cantidad);
            //DB.AddInParameter(command, "@TamanoFinal", DbType.Decimal, row.IsTamanoFinalNull() ? Convert.DBNull : row.TamanoFinal);
            //DB.AddInParameter(command, "@Comentarios", DbType.String, row.IsComentariosNull() ? Convert.DBNull : row.Comentarios);
            //DB.AddInParameter(command, "@EstadoId", DbType.String, row.IsEstadoIdNull() ? Convert.DBNull : row.EstadoId);
            //return (int)DB.ExecuteScalar(command, t);
            return 0;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "t"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "row"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "reqId")]
        private static void SaveReqDetalle(BERequisicion.RequisicionDetalleRow row, int reqId, DbTransaction t)
        {
            //var command = DB.GetStoredProcCommand(SaveReqDetalleSP);
            //DB.AddInParameter(command, "@empresaId", DbType.Int32, NortecMembership.CompanyId);
            //DB.AddInParameter(command, "@OTId", DbType.Int32, ordenId);
            //DB.AddInParameter(command, "@fechaInicial", DbType.DateTime, row.IsFechaInicialNull() ? Convert.DBNull : row.FechaInicial);
            //DB.AddInParameter(command, "@fechaFinal", DbType.DateTime, row.IsFechaFinalNull() ? Convert.DBNull : row.FechaFinal);
            //DB.AddInParameter(command, "@UnidadesSolicitadas", DbType.Decimal, row.IsUnidadesSolicitadasNull() ? Convert.DBNull : row.UnidadesSolicitadas);
            //DB.AddInParameter(command, "@UnidadesProducidas", DbType.Decimal, row.IsUnidadesProducidasNull() ? Convert.DBNull : row.UnidadesProducidas);
            //DB.AddInParameter(command, "@ManoObraIndirecta", DbType.Decimal, row.IsManoObraIndirectaNull() ? Convert.DBNull : row.ManoObraIndirecta);
            //DB.AddInParameter(command, "@Orden", DbType.String, row.IsOrdenNull() ? Convert.DBNull : row.Orden);
            //DB.AddInParameter(command, "@EstadoId", DbType.String, row.IsEstadoIdNull() ? Convert.DBNull : row.EstadoId);
            //return (int)DB.ExecuteScalar(command, t);
        }
        
        private static int SaveOTEntrega(BEOT.OTEntregaRow row, DbTransaction t)
        {
            var command = DB.GetStoredProcCommand(SaveOTEntregaSP);
            DB.AddInParameter(command, "@id", DbType.Int32, row.Id <= 0 ? Convert.DBNull : row.Id);
            DB.AddInParameter(command, "@ManoObraIndirecta", DbType.Decimal, row.IsManoObraIndirectaNull() ? Convert.DBNull : row.ManoObraIndirecta);
            DB.AddInParameter(command, "@UnidadesProducidas", DbType.Decimal, row.IsUnidadesProducidasNull() ? Convert.DBNull : row.UnidadesProducidas);

            return (int)DB.ExecuteScalar(command, t);
        }

        private static void SaveOTDetalle(BEOT.OTDetalleRow row, int entregaId, DbTransaction t)
        {
            var command = DB.GetStoredProcCommand(SaveOTDetalleSP);

            DB.AddInParameter(command, "@OTEntregaId", DbType.Int32, entregaId);
            DB.AddInParameter(command, "@fecha", DbType.DateTime, row.Fecha);
            DB.AddInParameter(command, "@operarioId", DbType.String, row.IsOperarioIdNull() ? Convert.DBNull : row.OperarioId);
            DB.AddInParameter(command, "@ayudanteId", DbType.String, row.IsAyudanteIdNull() ? Convert.DBNull : row.AyudanteId);
            DB.AddInParameter(command, "@maquinaId", DbType.String, row.IsMaquinaIdNull() ? Convert.DBNull : row.MaquinaId);
            DB.AddInParameter(command, "@cantidad", DbType.Decimal, row.IsCantidadNull() ? Convert.DBNull : row.Cantidad);
            DB.AddInParameter(command, "@horasRegular", DbType.Decimal, row.IsHorasRegularNull() ? Convert.DBNull : row.HorasRegular);
            DB.AddInParameter(command, "@horasExtra", DbType.Decimal, row.IsHorasExtraNull() ? Convert.DBNull : row.HorasExtra);
            DB.AddInParameter(command, "@horasDoble", DbType.Decimal, row.IsHorasDobleNull() ? Convert.DBNull : row.HorasDoble);
            DB.AddInParameter(command, "@tipo", DbType.String, row.IsTipoNull() ? Convert.DBNull : row.Tipo);
            DB.AddInParameter(command, "@descripcion", DbType.String, row.IsDescripcionNull() ? Convert.DBNull : row.Descripcion);

            DB.ExecuteNonQuery(command, t);
        }

        private static void SaveOTMaterial(BEOT.OTMaterialRow row, int entregaId, DbTransaction t)
        {
            var command = DB.GetStoredProcCommand(SaveOTMaterialSP);

            DB.AddInParameter(command, "@OTEntregaId", DbType.Int32, entregaId);
            DB.AddInParameter(command, "@empresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@fecha", DbType.DateTime, row.Fecha);
            DB.AddInParameter(command, "@tipo", DbType.String, row.IsTipoNull() ? Convert.DBNull : row.Tipo);
            DB.AddInParameter(command, "@operarioId", DbType.String, row.IsOperarioIdNull() ? Convert.DBNull : row.OperarioId);
            DB.AddInParameter(command, "@articuloId", DbType.String, row.IsMaterialIdNull() ? Convert.DBNull : row.MaterialId);
            DB.AddInParameter(command, "@cantidad", DbType.Decimal, row.IsCantidadNull() ? Convert.DBNull : row.Cantidad);
            DB.AddInParameter(command, "@departamento", DbType.String, row.IsDepartamentoNull() ? Convert.DBNull : row.Departamento);

            DB.ExecuteNonQuery(command, t);
        }

        private static void SaveOTGasto(BEOT.OTGastoRow row, int entregaId, DbTransaction t)
        {
            var command = DB.GetStoredProcCommand(SaveOTGastoSP);

            DB.AddInParameter(command, "@OTEntregaId", DbType.Int32, entregaId);
            DB.AddInParameter(command, "@gastoId", DbType.Int32, row.GastoId);
            DB.AddInParameter(command, "@monto", DbType.Decimal, row.IsMontoNull() ? Convert.DBNull : row.Monto);

            DB.ExecuteNonQuery(command, t);
        }
        
        private static BEOT ConvertRawToEntity(DataSet rawDataSet)
        {
            var entity = new BEOT {EnforceConstraints = false};

            for (var i = 1; i < rawDataSet.Tables.Count; i++)
                rawDataSet.Tables[i].TableName = rawDataSet.Tables[0].Rows[i]["TableName"].ToString();

            entity.Merge(rawDataSet);
            return entity;
        }

        private static BEHorasLaboradasReporte ConvertRawToHorasLaboradas(DataSet rawDataSet)
        {
            var entity = new BEHorasLaboradasReporte {EnforceConstraints = false};

            for (var i = 1; i < rawDataSet.Tables.Count; i++)
                rawDataSet.Tables[i].TableName = rawDataSet.Tables[0].Rows[i]["TableName"].ToString();

            entity.Merge(rawDataSet);

            return entity;
        }

        #endregion
    }
}
